//
//  LoginViewController.m
//  DebbyDemo
//
//  Created by wanghao on 16/4/23.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "LoginViewController.h"

#import <MBProgressHUD/MBProgressHUD.h>
#import <AVOSCloud.h>



@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UITextField *nickNameTF;
@property (weak, nonatomic) IBOutlet UITextField *verifyTF;
@property (weak, nonatomic) IBOutlet UITextField *emailTF;

@property (weak, nonatomic) IBOutlet UITextField *loginPhoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *loginPwdTF;

//重置空间
@property (weak, nonatomic) IBOutlet UITextField *rePhoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *reVerifyTF;
@property (weak, nonatomic) IBOutlet UITextField *reNewPwdTF;

@property (weak, nonatomic) IBOutlet UITextField *resetMailTF;

@property (nonatomic,retain) MBProgressHUD *hud;//加载小菊花



@end

@implementation LoginViewController

//懒加载
- (MBProgressHUD *)hud
{
    if (!_hud)
    {
        _hud = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:_hud];
        //样式
        _hud.mode = MBProgressHUDModeAnnularDeterminate;
        //    HUD.delegate = self;
       
    }
    return _hud;
}

#pragma mark -- 注册

- (void)registerByNameAndEmail
{
    //用户名和密码注册
     
     AVUser *user = [AVUser user];// 新建 AVUser 对象实例
     user.username = @"Tom";// 设置用户名
     user.password =  @"cat!@#123";// 设置密码
     user.email = @"tom@leancloud.cn";// 设置邮箱
     
     [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
     if (succeeded) {
     // 注册成功
     } else {
     // 失败的原因可能有多种，常见的是用户名已经存在。
     }
     }];
     
}

//注册按钮
- (IBAction)registerAction:(id)sender {
    
    self.hud.labelText = @"加载中";
    [self.hud show:YES];
    
    //测试消息
    AVObject *testObject = [AVObject objectWithClassName:@"register"];
    [testObject setObject:@"bar" forKey:@"注册点击"];
    [testObject save];
    
    AVUser *user = [AVUser user];
    user.username = _nickNameTF.text;
    user.password =  _pwdTF.text;
    user.email = _emailTF.text;
    user.mobilePhoneNumber = _phoneNumTF.text;
    NSError *error = nil;
    
    
    //注册操作
    [user signUp:&error];
    
    NSLog(@"注册结果: 空为成功 - %@",error);
    if (!error) {
        _hud.labelText = @"注册成功";
        [_hud show:YES];
        [_hud hide:YES afterDelay:1];
    }
//    
//    //用验证码和手机号注册
//    [AVUser signUpOrLoginWithMobilePhoneNumberInBackground:_phoneNumTF.text smsCode:_verifyTF.text block:^(AVUser *user, NSError *error) {
//        // 如果 error 为空就可以表示登录成功了，并且 user 是一个全新的用户
//        if (!error) {
//            NSLog(@"reg success");
//        }
//        else
//        {
//            NSLog(@"reg fs %@",error);
//        }
//    }];
//    
    
    [_hud hide:YES];
}

//发送验证码
- (IBAction)sendVerifyCode:(id)sender {
    //    [AVOSCloud requestSmsCodeWithPhoneNumber:_phoneNumTF.text callback:^(BOOL succeeded, NSError *error) {
    //        // 发送失败可以查看 error 里面提供的信息
    //        NSLog(@"%@",error);
    //    }];
    //
    [AVOSCloud requestSmsCodeWithPhoneNumber:_phoneNumTF.text
                                     appName:@"悦动"
                                   operation:@"您进行了注册操作"
                                  timeToLive:10
                                    callback:^(BOOL succeeded, NSError *error) {
                                        if (succeeded) {
                                            // 发送成功
                                            //短信格式类似于：
                                            //您正在{某应用}中进行{具体操作名称}，您的验证码是:{123456}，请输入完整验证，有效期为:{10}分钟
                                            NSLog(@"验证短信发送成功");
                                        }
                                    }];
}
//登录按钮
- (IBAction)loginAction:(id)sender {
    
    self.hud.labelText = @"登录...";
    [self.hud show:YES];
    [AVUser logInWithMobilePhoneNumberInBackground:_loginPhoneNumTF.text password:_loginPwdTF.text block:^(AVUser *user, NSError *error) {
        if (user!= nil) {
            NSLog(@"login success");
            NSLog(@"%@",user.mobilePhoneNumber);
        }
        else
        {
            NSLog(@"%@",error);
        }
        [self.hud hide:YES];
    }];
}

#pragma mark -- 重置密码

//通过邮箱重置密码
- (IBAction)resetPwdByMail:(id)sender {
    [AVUser requestPasswordResetForEmailInBackground:_resetMailTF.text block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"重置消息发送邮件成功");
            _hud.labelText = @"邮件已发送";
            [self.hud show:YES];
            [_hud hide:YES afterDelay:1];
            
        } else {
            NSLog(@"%@",error);
        }
    }];
}

//请求验证码
- (IBAction)getVerifyNum:(id)sender {
    [AVUser requestPasswordResetForEmailInBackground:_resetMailTF.text block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            
        } else {
            
        }
    }];
}

//通过手机号重置密码,发送验证码和新的密码
- (IBAction)resetByPhoneNum:(id)sender {
    
    [AVUser resetPasswordWithSmsCode:_reVerifyTF.text newPassword:_reNewPwdTF.text block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
        } else {
            
        }
    }];
}

#pragma mark -- 功能切换
//返回登录
- (IBAction)backToLoginFromRegister:(id)sender {
    [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
}
//去注册界面
- (IBAction)toRegister:(id)sender {
    [self.scrollView setContentOffset:CGPointMake(kWidth*2, 0) animated:YES];
}
//去忘记密码界面
- (IBAction)toLostPwd:(id)sender {
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"登录";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //设置scrollView起始在中间
    [self.view layoutIfNeeded];
    
    [self.scrollView setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width, 0)];

    NSLog(@"%f,%f",kWidth,_scrollView.frame.size.width);
}

//返回方法
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
